﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2009 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2009-2-6 15:57:09
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IDAL;
using DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace SQLServerDAL
{
    /// <summary>
    /// 数据访问类 InergalExtend
    /// </summary>
    public class InergalExtend : IInergalExtend
    {
        public InergalExtend()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(Model.InergalExtend model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [InergalExtend](");
            strSql.Append("[InergalCarId],[IntergalRange1],[IntergalRange2],[InergalExtendAttribute1],[InergalExtendAttribute2],[InergalExtendAttribute3],[InergalExtendAttribute4],[InergalExtendAttribute5],[InergalExtendAttribute6],[InergalExtendAttribute7],[InergalExtendAttribute8])");
            strSql.Append(" values (");
            strSql.Append("@InergalCarId,@IntergalRange1,@IntergalRange2,@InergalExtendAttribute1,@InergalExtendAttribute2,@InergalExtendAttribute3,@InergalExtendAttribute4,@InergalExtendAttribute5,@InergalExtendAttribute6,@InergalExtendAttribute7,@InergalExtendAttribute8)");
            SqlParameter[] parameters = {
                    new SqlParameter("@InergalCarId", SqlDbType.VarChar,24),
                    new SqlParameter("@IntergalRange1", SqlDbType.Decimal),
                    new SqlParameter("@IntergalRange2", SqlDbType.Decimal),
                    new SqlParameter("@InergalExtendAttribute1", SqlDbType.VarChar,50),
                    new SqlParameter("@InergalExtendAttribute2", SqlDbType.VarChar,50),
                    new SqlParameter("@InergalExtendAttribute3", SqlDbType.VarChar,100),
                    new SqlParameter("@InergalExtendAttribute4", SqlDbType.VarChar,100),
                    new SqlParameter("@InergalExtendAttribute5", SqlDbType.Int),
                    new SqlParameter("@InergalExtendAttribute6", SqlDbType.Int),
                    new SqlParameter("@InergalExtendAttribute7", SqlDbType.Decimal),
                    new SqlParameter("@InergalExtendAttribute8", SqlDbType.Decimal)
            };
            parameters[0].Value = model.InergalCarId;
            parameters[1].Value = model.IntergalRange1;
            parameters[2].Value = model.IntergalRange2;

            if (model.InergalExtendAttribute1 != null)
                parameters[3].Value = model.InergalExtendAttribute1;
            else
                parameters[3].Value = DBNull.Value;


            if (model.InergalExtendAttribute2 != null)
                parameters[4].Value = model.InergalExtendAttribute2;
            else
                parameters[4].Value = DBNull.Value;


            if (model.InergalExtendAttribute3 != null)
                parameters[5].Value = model.InergalExtendAttribute3;
            else
                parameters[5].Value = DBNull.Value;


            if (model.InergalExtendAttribute4 != null)
                parameters[6].Value = model.InergalExtendAttribute4;
            else
                parameters[6].Value = DBNull.Value;

            parameters[7].Value = model.InergalExtendAttribute5;
            parameters[8].Value = model.InergalExtendAttribute6;
            parameters[9].Value = model.InergalExtendAttribute7;
            parameters[10].Value = model.InergalExtendAttribute8;

            if (SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0)
                return model.InergalCarId;
            else
                return null;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.InergalExtend model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [InergalExtend] set ");
            strSql.Append("[IntergalRange1]=@IntergalRange1,");
            strSql.Append("[IntergalRange2]=@IntergalRange2,");
            strSql.Append("[InergalExtendAttribute1]=@InergalExtendAttribute1,");
            strSql.Append("[InergalExtendAttribute2]=@InergalExtendAttribute2,");
            strSql.Append("[InergalExtendAttribute3]=@InergalExtendAttribute3,");
            strSql.Append("[InergalExtendAttribute4]=@InergalExtendAttribute4,");
            strSql.Append("[InergalExtendAttribute5]=@InergalExtendAttribute5,");
            strSql.Append("[InergalExtendAttribute6]=@InergalExtendAttribute6,");
            strSql.Append("[InergalExtendAttribute7]=@InergalExtendAttribute7,");
            strSql.Append("[InergalExtendAttribute8]=@InergalExtendAttribute8");
            strSql.Append(" where [InergalCarId]=@InergalCarId");
            SqlParameter[] parameters = {
                    new SqlParameter("@InergalCarId", SqlDbType.VarChar,24),
                    new SqlParameter("@IntergalRange1", SqlDbType.Decimal),
                    new SqlParameter("@IntergalRange2", SqlDbType.Decimal),
                    new SqlParameter("@InergalExtendAttribute1", SqlDbType.VarChar,50),
                    new SqlParameter("@InergalExtendAttribute2", SqlDbType.VarChar,50),
                    new SqlParameter("@InergalExtendAttribute3", SqlDbType.VarChar,100),
                    new SqlParameter("@InergalExtendAttribute4", SqlDbType.VarChar,100),
                    new SqlParameter("@InergalExtendAttribute5", SqlDbType.Int),
                    new SqlParameter("@InergalExtendAttribute6", SqlDbType.Int),
                    new SqlParameter("@InergalExtendAttribute7", SqlDbType.Decimal),
                    new SqlParameter("@InergalExtendAttribute8", SqlDbType.Decimal)
            };
            parameters[0].Value = model.InergalCarId;
            parameters[1].Value = model.IntergalRange1;
            parameters[2].Value = model.IntergalRange2;

            if (model.InergalExtendAttribute1 != null)
                parameters[3].Value = model.InergalExtendAttribute1;
            else
                parameters[3].Value = DBNull.Value;


            if (model.InergalExtendAttribute2 != null)
                parameters[4].Value = model.InergalExtendAttribute2;
            else
                parameters[4].Value = DBNull.Value;


            if (model.InergalExtendAttribute3 != null)
                parameters[5].Value = model.InergalExtendAttribute3;
            else
                parameters[5].Value = DBNull.Value;


            if (model.InergalExtendAttribute4 != null)
                parameters[6].Value = model.InergalExtendAttribute4;
            else
                parameters[6].Value = DBNull.Value;

            parameters[7].Value = model.InergalExtendAttribute5;
            parameters[8].Value = model.InergalExtendAttribute6;
            parameters[9].Value = model.InergalExtendAttribute7;
            parameters[10].Value = model.InergalExtendAttribute8;

            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string InergalCarId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete [InergalExtend] ");
            strSql.Append(" where [InergalCarId]=@InergalCarId");
            SqlParameter[] parameters = {
                    new SqlParameter("@InergalCarId", SqlDbType.VarChar,24)};
            parameters[0].Value = InergalCarId;

            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string InergalCarId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [InergalExtend]");
            strSql.Append(" where [InergalCarId]= @InergalCarId");
            SqlParameter[] parameters = {
                    new SqlParameter("@InergalCarId", SqlDbType.VarChar,24)};
            parameters[0].Value = InergalCarId;
            return SqlHelper.Exists(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.InergalExtend GetModel(string InergalCarId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [InergalExtend] ");
            strSql.Append(" where [InergalCarId]=@InergalCarId");
            SqlParameter[] parameters = {
                    new SqlParameter("@InergalCarId", SqlDbType.VarChar,24)};
            parameters[0].Value = InergalCarId;
            DataSet ds = SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                return GetModel(r);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取数据集,建议只在多表联查时使用
        /// </summary>
        public DataSet GetDataSet(string strSql)
        {
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql);
        }

        /// <summary>
        /// 获取泛型数据列表,建议只在多表联查时使用
        /// </summary>
        public IList<Model.InergalExtend> GetList(string strSql)
        {
            return GetList(GetDataSet(strSql));
        }

        /// <summary>
        /// 获取数据集,在单表查询时使用
        /// </summary>
        public DataSet GetDataSet(string strWhat, string strWhere, string strOrderby)
        {
            if (string.IsNullOrEmpty(strWhat))
                strWhat = "*";
            StringBuilder strSql = new StringBuilder("select " + strWhat + " from [InergalExtend]");
            if (!string.IsNullOrEmpty(strWhere))
                strSql.Append(" where " + strWhere);
            if (!string.IsNullOrEmpty(strOrderby))
                strSql.Append(" order by " + strOrderby);
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString());
        }

        /// <summary>
        /// 获取泛型数据列表,在单表查询时使用
        /// </summary>
        public IList<Model.InergalExtend> GetList(string strWhat, string strWhere, string strOrderby)
        {
            return GetList(GetDataSet(strWhat, strWhere, strOrderby));
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public IList<Model.InergalExtend> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            string strSql;
            DataSet ds = SqlHelper.PageList(SqlHelper.LocalSqlServer, "[InergalExtend]", null, pageSize, pageIndex, fldSort, sort, strCondition, "InergalCarId", false, out pageCount, out count, out strSql);
            return GetList(ds);
        }
        #endregion

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.InergalExtend GetModel(DataRow r)
        {
            Model.InergalExtend model = new Model.InergalExtend();
            model.InergalCarId = SqlHelper.GetString(r["InergalCarId"]);
            model.IntergalRange1 = SqlHelper.GetDecimal(r["IntergalRange1"]);
            model.IntergalRange2 = SqlHelper.GetDecimal(r["IntergalRange2"]);
            model.InergalExtendAttribute1 = SqlHelper.GetString(r["InergalExtendAttribute1"]);
            model.InergalExtendAttribute2 = SqlHelper.GetString(r["InergalExtendAttribute2"]);
            model.InergalExtendAttribute3 = SqlHelper.GetString(r["InergalExtendAttribute3"]);
            model.InergalExtendAttribute4 = SqlHelper.GetString(r["InergalExtendAttribute4"]);
            model.InergalExtendAttribute5 = SqlHelper.GetInt(r["InergalExtendAttribute5"]);
            model.InergalExtendAttribute6 = SqlHelper.GetInt(r["InergalExtendAttribute6"]);
            model.InergalExtendAttribute7 = SqlHelper.GetDecimal(r["InergalExtendAttribute7"]);
            model.InergalExtendAttribute8 = SqlHelper.GetDecimal(r["InergalExtendAttribute8"]);
            return model;
        }

        /// <summary>
        /// 由数据集得到泛型数据列表
        /// </summary>
        private IList<Model.InergalExtend> GetList(DataSet ds)
        {
            List<Model.InergalExtend> l = new List<Model.InergalExtend>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                l.Add(GetModel(r));
            }
            return l;
        }
    }
}


